import os

try:
  from utils import tracer
except:
  pass

class ProjectItemType:
  Undefine = 0x000
  Folder = 0x100
  File = 0x200
  PyFile = 0x201
  CfgFile = 0x202
  IniFile = 0x203
  XmlFile = 0x204
  ImgFile = 0x205


class ProjectItem(object):
  def __init__(self, name, item_type, parent):
    self._name = name
    self._type = item_type
    self._parent = parent
    self._subItems = []

  def getName(self):
    return self._name

  def getType(self):
    return self._type

  def getItems(self):
    return self._subItems

  def clearItems(self):
    self._subItems = []

  def getItemByName(self, name):
    for item in self.getItems():
      if item.getName() == name:
        return item
    return None

  def getParent(self):
    return self._parent

  def getFullname(self):
    fullname = self.getName()
    item = self
    while not item.getParent() is None:
      item = item.getParent()
      if type(item) is ProjectInfo:
        fullname = '%s%s%s' % (item.getPath(), os.sep, fullname)
        return fullname
      else:
        fullname = '%s%s%s' % (item.getName(), os.sep, fullname)
    return fullname

class ProjectInfo(object):  
  def __init__(self, name='', path=''):
    self._version = ''
    self._author = ''
    self._lisence = ''
    self._mainScript = ''
    self._description = ''
    self._items = []
    self._name = name
    self._path = os.path.normpath(path)

  def getName(self):
    return self._name
  def setName(self, name):
    self._name = name

  def getPath(self):
    return self._path
  def setPath(self, path):
    self._path = os.path.normpath(path)

  def getVersion(self):
    return self._version
  def setVersion(self, version):
    self._version = version

  def getAuthor(self):
    return self._author
  def setAuthor(self, author):
    self._author = author

  def getLisence(self):
    return self._lisence
  def setLisence(self, lisence):
    self._lisence = lisence

  def getMainScript(self):
    return self._mainScript
  def setMainScript(self, mainScript):
    self._mainScript = mainScript

  def getDescription(self):
    return self._description
  def setDescription(self, description):
    self._description = description

  def getItems(self):
    return self._items

  def getItemByName(self, name):
    for item in self.getItems():
      if item.getName() == name:
        return item
    return None

  def addItem(self, path):
    assert os.path.exists(path)

    dirname = os.path.dirname(path)
    basename = os.path.basename(path)

    item = self.findItemByPath(dirname)
    if item is None:
      return None

    newItem = ProjectItem(basename, self.getItemType(path), item)
    item.getItems().append(newItem)

    return newItem

  def removeItem(self, path):
    assert os.path.exists(path)
    removeItem = self.findItemByPath(path)
    del removeItem
    return True

  def getItemType(self, path):
    assert os.path.exists(path)

    itemType = ProjectItemType.File

    if os.path.isdir(path):
      itemType = ProjectItemType.Folder
    elif os.path.isfile(path):
      splitedName = path.split(os.path.extsep)
      if len(splitedName) > 1:
        if splitedName[1].lower() == 'py':
          itemType = ProjectItemType.PyFile
        elif splitedName[1].lower() == 'cfg':
          itemType = ProjectItemType.CfgFile
        elif splitedName[1].lower() == 'ini':
          itemType = ProjectItemType.IniFile
        elif splitedName[1].lower() == 'xml':
          itemType = ProjectItemType.XmlFile
        elif splitedName[1].lower() == 'png' or splitedName[1].lower() == 'bmp' or splitedName[1].lower() == 'ico' or splitedName[1].lower() == 'jpg':
          itemType = ProjectItemType.ImgFile

    return itemType

  def findItemByPath(self, path):
    assert os.path.exists(path)

    dirname = os.path.dirname(path)
    basename = os.path.basename(path)

    projPath = self.getPath()

    if len(dirname) == len(projPath):
      return self.getItemByName(basename)

    subPath = dirname.replace(projPath + os.path.sep, '')
    dirnames = subPath.split(os.path.sep)

    itemType = ProjectItemType.File
    if os.path.isdir(path):
      itemType = ProjectItemType.Folder

    tracer.writeln('Project Path: %s, Target Path: %s' % (projPath, dirname))

    dirnames = []
    if subPath != '':
      dirnames = subPath.split(os.path.sep)

    tracer.writeln('dirname: %s, filename: %s' % (subPath, basename))

    item = self
    for dirname in dirnames:
      tracer.writeln('dirname: %s' % dirname)
      item = item.getItemByName(dirname)
      if item is None:
        tracer.writeln('Special item not found.%sItem:%s' % (os.linesep, dirname))
        return None

    return item.getItemByName(basename)

  def dump(self):
    if dir().__contains__('tracer'):
      tracer.writeln('%s: %s' % (self.getName(), self.getPath()))
      
    self.dumpItem(self.getItems(), 1)

  def dumpItem(self, lst, lvl):
    for item in lst:
      if dir().__contains__('tracer'):
        tracer.writeln('%s+- %s: %s' % (' ' * lvl, item.getName(), item.getFullname()))
        
      if len(item.getItems()) > 0:
        self.dumpItem(item.getItems(), lvl + 1)
